package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.pojo.Keyword;
import com.cskaoyan.bean.pojo.KeywordExample;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.service.admin.KeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Name : KeywordServiceImpl.java
 * @Time : 2021/8/13 11:45
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */

@Service
@Transactional
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public BaseRespData list(String keyword, String url, BaseParamBo baseParamBo) {
        PageHelper.startPage(baseParamBo.getPage(), baseParamBo.getLimit());
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(baseParamBo.getSort() + " " + baseParamBo.getOrder());
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (keyword != null && !keyword.equals("")) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null && !url.equals("")) {
            criteria.andUrlLike("%" + url + "%");
        }
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        long total = new PageInfo<>(keywords).getTotal();
        return BaseRespData.create(keywords, total);
    }

    @Override
    public Keyword create(Keyword keyword) {
        Date date = new Date();
        keyword.setAddTime(date);
        keyword.setUpdateTime(date);
        keyword.setDeleted(false);
        keywordMapper.insertSelective(keyword);
        keyword.setId(keyword.getId());
        return keyword;
    }

    @Override
    public Keyword update(Keyword keyword) {
        Date date = new Date();
        keyword.setUpdateTime(date);
        keywordMapper.updateByPrimaryKeySelective(keyword);
        return keyword;
    }

    @Override
    public void delete(Keyword keyword) {
        keyword.setDeleted(true);
        keywordMapper.updateByPrimaryKeySelective(keyword);
    }
}
